package fr.efrei.tradesys.struts.interceptors;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

import fr.efrei.tradesys.hibernate.beans.AppUser;

public class CheckAdminOrConcernedInterceptor implements Interceptor
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static Log				LOGGER						= LogFactory
																									.getLog(CheckAdminOrConcernedInterceptor.class);

	public void destroy()
	{
	}

	public void init()
	{
	}

	public String intercept(ActionInvocation action) throws Exception
	{
		LOGGER.info("Execution of " + this.getClass().getName());
		Map<String, Object> session = ActionContext.getContext().getSession();
		AppUser u = (AppUser) session.get("user");
		if (u != null && u.getAdmin())
			return action.invoke();
		String[] loginParameter = ((String[]) action.getInvocationContext()
				.getParameters().get("login"));
		if (loginParameter != null && loginParameter[0].equals(u.getLogin()))
			return action.invoke();
		return Action.LOGIN;
	}

}
